home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Found / FWRunTyp / Include / FWClaInf.h next >
Encoding:
Text File  |  1994-04-21  |  10.8 KB  |  311 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWClaInf.h
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Creation Date:        3/28/94
  7. //
  8. //    Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #ifndef FWCLAINF_H
  13. #define FWCLAINF_H
  14.  
  15. #include <stddef.h>
  16.  
  17. #ifndef FWSTDDEF_H
  18. #include "FWStdDef.h"
  19. #endif
  20.  
  21. #ifdef FW_BUILD_WIN32S
  22. #include <Windows.h>
  23. #endif
  24.  
  25. //========================================================================================
  26. // FW_CAN_EXPORT_DATA
  27. //========================================================================================
  28.  
  29. #if defined(FW_BUILD_MAC) || defined(FW_BUILD_WIN16)
  30. #define FW_CAN_EXPORT_DATA
  31. #endif
  32.  
  33. //========================================================================================
  34. // CLASS FW_CClassInfo
  35. //========================================================================================
  36.  
  37. class FW_CClassInfo;
  38. typedef const FW_CClassInfo *FW_ClassReference;
  39.  
  40. class FW_CClassInfo
  41. {
  42. public:
  43.  
  44.     ~FW_CClassInfo();
  45.     FW_CClassInfo(const char *const className,
  46.                     size_t instanceSize, 
  47.                     FW_ClassReference  const *ancestors);
  48.     
  49.     const char *const GetClassName() const;
  50.         // Returns a char string containing name of the class
  51.  
  52.     const size_t GetInstanceSize() const;
  53.         // Returns the size of instances of the class
  54.  
  55.     FW_ClassReference  const * GetAncestors() const;
  56.         // Returns the list of ancestors of the class
  57.  
  58.     long GetHashKey() const;
  59.         // Returns the hash key for the class name
  60.  
  61.     FW_Boolean IsBaseOf(FW_ClassReference aDerivedClass) const;
  62.         // Return true if the class is or a base of the class represented by aDerivedClass
  63.         // Note, usage is: this->IsBaseOf(aDerivedClass)
  64.  
  65.     FW_Boolean IsKindOf(FW_ClassReference aBaseClass) const;
  66.         // Return true if the class is or inherits from class represented by aBaseClass
  67.         // Note, usage is: this->IsKindOf(aBaseClass)
  68.     
  69.     int operator==(const FW_CClassInfo&) const;
  70.     int operator!=(const FW_CClassInfo&) const;
  71.  
  72. private:
  73.  
  74.     const char *const fClassName;
  75.         // Name of the class
  76.  
  77.     size_t fInstanceSize;
  78.         // Size of instances of the class
  79.         
  80.     FW_ClassReference  const *fAncestors;
  81.         // ClassInfo for ancestors of the class.
  82.         // Pointer to array of const pointers to const FW_CClassInfo
  83.     
  84.     FW_CClassInfo(const FW_CClassInfo&);
  85.     FW_CClassInfo& operator=(const FW_CClassInfo&);
  86.         // ClassInfo objects cannot be copied
  87.         
  88. };
  89.  
  90. //----------------------------------------------------------------------------------------
  91. //    FW_CClassInfo::operator==
  92. //----------------------------------------------------------------------------------------
  93.  
  94. inline int FW_CClassInfo::operator==(const FW_CClassInfo& other) const
  95. {
  96.     return &other == this;
  97. }
  98.     
  99. //----------------------------------------------------------------------------------------
  100. //    FW_CClassInfo::operator!=
  101. //----------------------------------------------------------------------------------------
  102.  
  103. inline int FW_CClassInfo::operator!=(const FW_CClassInfo& other) const
  104. {
  105.     return &other != this;
  106. }
  107.  
  108. //----------------------------------------------------------------------------------------
  109. //    FW_CClassInfo::GetClassName
  110. //----------------------------------------------------------------------------------------
  111.  
  112. inline const char *const FW_CClassInfo::GetClassName() const
  113. {
  114.     return fClassName;
  115. }
  116.  
  117. //----------------------------------------------------------------------------------------
  118. //    FW_CClassInfo::GetInstanceSize
  119. //----------------------------------------------------------------------------------------
  120.  
  121. inline const size_t FW_CClassInfo::GetInstanceSize() const
  122. {
  123.     return fInstanceSize;
  124. }
  125.  
  126. //----------------------------------------------------------------------------------------
  127. //    FW_CClassInfo::GetAncestors
  128. //----------------------------------------------------------------------------------------
  129.  
  130. inline FW_ClassReference  const * FW_CClassInfo::GetAncestors() const
  131. {
  132.     return fAncestors;
  133. }
  134.  
  135. //----------------------------------------------------------------------------------------
  136. //    FW_CClassInfo::IsBaseOf
  137. //----------------------------------------------------------------------------------------
  138.  
  139. inline FW_Boolean FW_CClassInfo::IsBaseOf(FW_ClassReference aDerivedClass) const
  140. {
  141.     return aDerivedClass->IsKindOf(this);
  142. }
  143.  
  144. //========================================================================================
  145. // Macros for generating and accessing class information
  146. //========================================================================================
  147.  
  148. // ----- OPF internal macros.
  149. //       Clients should use the public RTTI macros defined at the end of this file
  150.  
  151. #ifdef FW_CAN_EXPORT_DATA
  152.     #define FW_CLASS_REFERENCE(name) \
  153.         (&name::gClassInfo)
  154. #else
  155.     #define FW_CLASS_REFERENCE(name) \
  156.         (&name::PrivStaticGetClassInfo())
  157. #endif
  158.  
  159. #define _FW_CLASS_ANCESTORS(name) \
  160.     name::gAncestors
  161.         
  162. #ifdef FW_CAN_EXPORT_DATA
  163.     #define _FW_DEFINE_CLASSINFO_OBJECT(name)                                             \
  164.         const FW_CClassInfo FW_SHARED_DATA                                                 \
  165.             name::gClassInfo(#name, sizeof(name), _FW_CLASS_ANCESTORS(name));
  166. #else
  167.     #define _FW_DEFINE_CLASSINFO_OBJECT(name)                                             \
  168.         const FW_CClassInfo FW_SHARED_DATA                                                \
  169.             name::gClassInfo(#name, sizeof(name), name::PrivGetAncestors());                \
  170.         const FW_CClassInfo& name::PrivStaticGetClassInfo() { return name::gClassInfo; }        \
  171.         const FW_ClassReference* name::PrivGetAncestors() { return name::gAncestors; }
  172. #endif
  173.  
  174. #define _FW_CLASS_ANCESTORS_IMPLEMENT_M0(name)                                             \
  175.     const FW_ClassReference FW_SHARED_DATA _FW_CLASS_ANCESTORS(name)[] =                \
  176.     {                                                                                    \
  177.         0                                                                                \
  178.     };
  179.  
  180. #define _FW_CLASS_ANCESTORS_IMPLEMENT_M1(name, ancestor)                                \
  181.     const FW_ClassReference FW_SHARED_DATA _FW_CLASS_ANCESTORS(name)[] =                \
  182.     {                                                                                    \
  183.         FW_CLASS_REFERENCE(ancestor),                                                    \
  184.         0                                                                                \
  185.     };
  186.  
  187. #define _FW_CLASS_ANCESTORS_IMPLEMENT_M2(name, ancestor1, ancestor2)                    \
  188.     const FW_ClassReference FW_SHARED_DATA _FW_CLASS_ANCESTORS(name)[] =                \
  189.     {                                                                                    \
  190.         FW_CLASS_REFERENCE(ancestor1),                                                    \
  191.         FW_CLASS_REFERENCE(ancestor2),                                                    \
  192.         0                                                                                \
  193.     };
  194.  
  195. #define _FW_CLASS_ANCESTORS_IMPLEMENT_M3(name, ancestor1, ancestor2, ancestor3)            \
  196.     const FW_ClassReference FW_SHARED_DATA _FW_CLASS_ANCESTORS(name)[] =                \
  197.     {                                                                                    \
  198.         FW_CLASS_REFERENCE(ancestor1),                                                    \
  199.         FW_CLASS_REFERENCE(ancestor2),                                                    \
  200.         FW_CLASS_REFERENCE(ancestor3),                                                    \
  201.         0                                                                                \
  202.     };
  203.  
  204. #define _FW_CLASS_ANCESTORS_IMPLEMENT_M4(name, ancestor1, ancestor2, ancestor3, ancestor4)\
  205.     const FW_ClassReference FW_SHARED_DATA _FW_CLASS_ANCESTORS(name)[] =                \
  206.     {                                                                                    \
  207.         FW_CLASS_REFERENCE(ancestor1),                                                    \
  208.         FW_CLASS_REFERENCE(ancestor2),                                                    \
  209.         FW_CLASS_REFERENCE(ancestor3),                                                    \
  210.         FW_CLASS_REFERENCE(ancestor4),                                                    \
  211.         0                                                                                \
  212.     };
  213.  
  214. #define _FW_VIRTUAL_CLASSINFO_ACCESSOR(name)                                            \
  215.     FW_ClassReference name::PrivVirtualGetClassInfo(void) const                                    \
  216.     {                                                                                    \
  217.         return FW_CLASS_REFERENCE(name);                                                \
  218.     }
  219.  
  220. // ----- The public macros for declaring and defining RTTI scaffolding
  221.  
  222. #ifdef FW_CAN_EXPORT_DATA
  223.     #define FW_DECLARE_CLASS                                                            \
  224.         virtual FW_ClassReference PrivVirtualGetClassInfo(void) const;                    \
  225.         static const FW_CClassInfo FW_SHARED_DATA gClassInfo;                            \
  226.         static const FW_ClassReference FW_SHARED_DATA gAncestors[];
  227. #else
  228.     #define FW_DECLARE_CLASS                                                            \
  229.         virtual FW_ClassReference PrivVirtualGetClassInfo(void) const;                    \
  230.         static const FW_CClassInfo& PrivStaticGetClassInfo();                            \
  231.         static const FW_ClassReference* PrivGetAncestors();                                \
  232. private:                                                                                \
  233.         static const FW_CClassInfo FW_SHARED_DATA gClassInfo;                            \
  234.         static const FW_ClassReference FW_SHARED_DATA gAncestors[];                        \
  235. public:
  236. #endif
  237.     
  238. #define FW_DEFINE_CLASS_M0(name)                                                        \
  239.     _FW_CLASS_ANCESTORS_IMPLEMENT_M0(name)                                                \
  240.     _FW_DEFINE_CLASSINFO_OBJECT(name)                                                    \
  241.     _FW_VIRTUAL_CLASSINFO_ACCESSOR(name)
  242.  
  243. #define FW_DEFINE_CLASS_M1(name, ancestor)                                                \
  244.     _FW_CLASS_ANCESTORS_IMPLEMENT_M1(name, ancestor)                                    \
  245.     _FW_DEFINE_CLASSINFO_OBJECT(name)                                                    \
  246.     _FW_VIRTUAL_CLASSINFO_ACCESSOR(name)
  247.  
  248. #define FW_DEFINE_CLASS_M2(name, ancestor1, ancestor2)                                    \
  249.     _FW_CLASS_ANCESTORS_IMPLEMENT_M2(name, ancestor1, ancestor2)                        \
  250.     _FW_DEFINE_CLASSINFO_OBJECT(name)                                                    \
  251.     _FW_VIRTUAL_CLASSINFO_ACCESSOR(name)
  252.  
  253. #define FW_DEFINE_CLASS_M3(name, ancestor1, ancestor2, ancestor3)                        \
  254.     _FW_CLASS_ANCESTORS_IMPLEMENT_M3(name, ancestor1, ancestor2, ancestor3)                \
  255.     _FW_DEFINE_CLASSINFO_OBJECT(name)                                                    \
  256.     _FW_VIRTUAL_CLASSINFO_ACCESSOR(name)
  257.  
  258. #define FW_DEFINE_CLASS_M4(name, ancestor1, ancestor2, ancestor3, ancestor4)            \
  259.     _FW_CLASS_ANCESTORS_IMPLEMENT_M4(name, ancestor1, ancestor2, ancestor3, ancestor4)    \
  260.     _FW_DEFINE_CLASSINFO_OBJECT(name)                                                    \
  261.     _FW_VIRTUAL_CLASSINFO_ACCESSOR(name)
  262.  
  263. //========================================================================================
  264. // RTTI Portability Macros
  265. //
  266. //    These macros provide a transition path until all compilers support RTTI.
  267. //========================================================================================
  268.  
  269. //----------------------------------------------------------------------------------------
  270. // Dynamic Cast Macro
  271. //----------------------------------------------------------------------------------------
  272.  
  273. #ifdef FW_COMPILER_SUPPORTS_RTTI
  274.     #define FW_DYNAMIC_CAST(T, p) dynamic_cast<T*>(p)
  275. #else
  276.     #define FW_DYNAMIC_CAST(T, p) \
  277.         ( (p) && (p)->PrivVirtualGetClassInfo()->IsKindOf(FW_CLASS_REFERENCE(T)) ? (T*)p : (T*)0 )
  278. #endif
  279.  
  280. //----------------------------------------------------------------------------------------
  281. // typeid macros
  282. //----------------------------------------------------------------------------------------
  283.  
  284. // Note, C++ typeid's are TypeInfo *references*.
  285. // OPF TYPEIDs are ClassInfo *references*
  286. // FW_ClassReference are ClassInfo *pointers*.
  287. // Yes, it's ugly, but fix it later. ???JEL
  288.  
  289. #ifdef FW_COMPILER_SUPPORTS_RTTI
  290.     #define FW_TYPEID_FROM_TYPE(type) (typeid(type))
  291.     #define FW_TYPEID_FROM_POINTER(p) (typeid(*(p)))
  292. #else
  293.     #define FW_TYPEID_FROM_TYPE(type) (*FW_CLASS_REFERENCE(type))
  294.     #define FW_TYPEID_FROM_POINTER(p) (*(p)->PrivVirtualGetClassInfo())
  295. #endif
  296.  
  297. //----------------------------------------------------------------------------------------
  298. // Class name macros
  299. //----------------------------------------------------------------------------------------
  300.  
  301. #ifdef FW_COMPILER_SUPPORTS_RTTI
  302.     #define FW_CLASSNAME_FROM_TYPE(type) (typeid(type).name())
  303.     #define FW_CLASSNAME_FROM_POINTER(p) (typeid(*(p)).name())
  304. #else
  305.     #define FW_CLASSNAME_FROM_TYPE(type) (FW_TYPEID_FROM_TYPE(type).GetClassName())
  306.     #define FW_CLASSNAME_FROM_POINTER(p) (FW_TYPEID_FROM_POINTER(p).GetClassName())
  307. #endif
  308.  
  309. #endif
  310.  
  311.